﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraTab;
using System.Windows.Forms;
using DevExpress.XtraTabbedMdi;
using DevExpress.XtraBars;

namespace System
{
    public static class TabsExt
    {
        static Dictionary<XtraTabControl, int> tabs = new Dictionary<XtraTabControl, int>();
        static Dictionary<XtraTabbedMdiManager, PopupMenu> menus = new Dictionary<XtraTabbedMdiManager, PopupMenu>();

        public static void SetTabMinSize(this XtraTabControl tabControl, int minSize)
        {
            if (tabControl != null)
            {
                if (!tabs.ContainsKey(tabControl))
                {
                    tabs.Add(tabControl, minSize);
                    tabControl.TabPages.CollectionChanged += new System.ComponentModel.CollectionChangeEventHandler(TabPages_CollectionChanged);
                    tabControl.Disposed += new EventHandler(tabControl_Disposed);
                }
                else
                {
                    tabs[tabControl] = minSize;
                }
            }
            foreach (XtraTabPage p in tabControl.TabPages)
                SetPageSize(p, minSize);
        }
        private static void SetPageSize(XtraTabPage page, int minSize)
        {
            page.TextChanged -= new EventHandler(page_TextChanged);
            
            string text = page.Text.Trim();
            if (minSize > 0)
            {
                while (text.Mesure(page.Font).Width < minSize)
                    text += " ";
            }
            page.Text = text;
            page.TextChanged += new EventHandler(page_TextChanged);
        }

        static void page_TextChanged(object sender, EventArgs e)
        {
            XtraTabPage page = sender as XtraTabPage;
            if (page != null && page.TabControl != null && tabs.ContainsKey(page.TabControl))
            {
                SetPageSize(page, tabs[page.TabControl]);
            }
        }
        static void tabControl_Disposed(object sender, EventArgs e)
        {
            XtraTabControl t = sender as XtraTabControl;
            if (t != null && tabs.ContainsKey(t))
            {
                t.TabPages.CollectionChanged -= TabPages_CollectionChanged;
                t.Disposed -= tabControl_Disposed;
                tabs.Remove(t);
            }
        }

        static void TabPages_CollectionChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e)
        {
            if (e.Action == System.ComponentModel.CollectionChangeAction.Add)
            {
                XtraTabPage page = e.Element as XtraTabPage;
                if (page != null && page.TabControl !=null && tabs.ContainsKey(page.TabControl))
                {
                    SetPageSize(page, tabs[page.TabControl]);
                }
            }
        }

        public static void AddTabMenu(this XtraTabbedMdiManager manager, string CloseThis = "Zamknij Tą", string CloseOthers = "Zamknij Inne", string CloseAll = "Zamknij Wszystkie")
        {
            if (manager != null)
            {
                RemoveTabMenu(manager);
                manager.MouseDown += new MouseEventHandler(manager_MouseDown);

                DevExpress.XtraBars.BarManager barManager;
                DevExpress.XtraBars.PopupMenu popupMenu;
                DevExpress.XtraBars.BarButtonItem bbiZamknijTa;
                DevExpress.XtraBars.BarButtonItem bbiZamknijInne;
                DevExpress.XtraBars.BarButtonItem bbiZamknijWszystkie;
                barManager = new DevExpress.XtraBars.BarManager();
                popupMenu = new DevExpress.XtraBars.PopupMenu();
                bbiZamknijTa = new DevExpress.XtraBars.BarButtonItem();
                bbiZamknijInne = new DevExpress.XtraBars.BarButtonItem();
                bbiZamknijWszystkie = new DevExpress.XtraBars.BarButtonItem();
                ((System.ComponentModel.ISupportInitialize)(barManager)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(popupMenu)).BeginInit();
                // 
                // barManager1
                // 
                barManager.Form = manager.MdiParent;
                barManager.Items.AddRange(new DevExpress.XtraBars.BarItem[] {
            bbiZamknijInne,
            bbiZamknijTa,
            bbiZamknijWszystkie});
                // 
                // bbiZamknijTa
                // 
                bbiZamknijTa.Caption = CloseThis;
                bbiZamknijTa.Id = 1;
                bbiZamknijTa.Name = "bbiZamknijTa";
                bbiZamknijTa.Tag = manager;
                bbiZamknijTa.Visibility = (CloseThis.IsNotNull() ? BarItemVisibility.Always : BarItemVisibility.Never);
                bbiZamknijTa.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(bbiZamknijTa_ItemClick);
                // 
                // bbiZamknijInne
                // 
                bbiZamknijInne.Caption = CloseOthers;
                bbiZamknijInne.Id = 2;
                bbiZamknijInne.Name = "bbiZamknijInne";
                bbiZamknijInne.Tag = manager;
                bbiZamknijInne.Visibility = (CloseOthers.IsNotNull() ? BarItemVisibility.Always : BarItemVisibility.Never);
                bbiZamknijInne.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(bbiZamknijInne_ItemClick);
                // 
                // bbiZamknijWszystkie
                // 
                bbiZamknijWszystkie.Caption = CloseAll;
                bbiZamknijWszystkie.Id = 3;
                bbiZamknijWszystkie.Name = "bbiZamknijWszystkie";
                bbiZamknijWszystkie.Tag = manager;
                bbiZamknijWszystkie.Visibility = (CloseAll.IsNotNull() ? BarItemVisibility.Always : BarItemVisibility.Never);
                bbiZamknijWszystkie.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(bbiZamknijWszystkie_ItemClick);
                // 
                // popupMenu2
                // 
                popupMenu.LinksPersistInfo.AddRange(new DevExpress.XtraBars.LinkPersistInfo[] {
            new DevExpress.XtraBars.LinkPersistInfo(bbiZamknijTa),
            new DevExpress.XtraBars.LinkPersistInfo(bbiZamknijInne),
            new DevExpress.XtraBars.LinkPersistInfo(bbiZamknijWszystkie)});
                popupMenu.Manager = barManager;
                popupMenu.Name = "popupMenu2";
                ((System.ComponentModel.ISupportInitialize)(popupMenu)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(barManager)).EndInit();

                menus.Add(manager, popupMenu);
            }
        }
        public static void RemoveTabMenu(this XtraTabbedMdiManager manager)
        {
            if (manager != null)
            {
                if (menus.ContainsKey(manager))
                {
                    menus[manager].Manager.Dispose();
                    menus[manager].Dispose();
                    manager.MouseDown -=new MouseEventHandler(manager_MouseDown);
                }
            }
        }

        private static void manager_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                XtraTabbedMdiManager manager = sender as XtraTabbedMdiManager;
                if (manager != null && menus.ContainsKey(manager))
                {
                    menus[manager].ShowPopup(Control.MousePosition);
                }
            }
        }

        private static void bbiZamknijTa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraTabbedMdiManager manager = e.Item.Tag as XtraTabbedMdiManager;
            if (manager != null)
            {
                if (manager.SelectedPage != null)
                    manager.SelectedPage.MdiChild.Close();
            }
        }

        private static void bbiZamknijInne_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
             XtraTabbedMdiManager manager = e.Item.Tag as XtraTabbedMdiManager;
             if (manager != null)
             {
                 List<DevExpress.XtraTabbedMdi.XtraMdiTabPage> l = new List<DevExpress.XtraTabbedMdi.XtraMdiTabPage>();
                 foreach (DevExpress.XtraTabbedMdi.XtraMdiTabPage page in manager.Pages)
                     if (manager.SelectedPage != page)
                         l.Add(page);
                 foreach (DevExpress.XtraTabbedMdi.XtraMdiTabPage p in l)
                     p.MdiChild.Close();
             }
        }

        private static void bbiZamknijWszystkie_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraTabbedMdiManager manager = e.Item.Tag as XtraTabbedMdiManager;
            if (manager != null)
            {
                List<DevExpress.XtraTabbedMdi.XtraMdiTabPage> l = new List<DevExpress.XtraTabbedMdi.XtraMdiTabPage>();
                foreach (DevExpress.XtraTabbedMdi.XtraMdiTabPage page in manager.Pages)
                    l.Add(page);
                foreach (DevExpress.XtraTabbedMdi.XtraMdiTabPage p in l)
                    p.MdiChild.Close();
            }
        }

    }
}
